package cn.dnf.org.service.impl;

import cn.dnf.basic.exception.BusinessException;
import cn.dnf.basic.util.BaiduAuditUtils;
import cn.dnf.org.domain.Employee;
import cn.dnf.org.domain.Shop;
import cn.dnf.org.domain.ShopLog;
import cn.dnf.org.dto.ShopDto;
import cn.dnf.org.mapper.EmployeeMapper;
import cn.dnf.org.mapper.ShopLogMapper;
import cn.dnf.org.mapper.ShopMapper;
import cn.dnf.org.service.IShopService;
import cn.dnf.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Resource
    private ShopMapper shopMapper;
    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private ShopLogMapper shopLogMapper;
    @Resource
    private JavaMailSender javaMailSender;
    @Override
    public void settlement(Shop shop) {
        //实现shop入驻流程
        //1.非空校验 spring中提供了一个类，处理string的
        if(StringUtils.isEmpty(shop.getName())
               ||StringUtils.isEmpty(shop.getAddress())
                 ||StringUtils.isEmpty(shop.getTel())

                ||StringUtils.isEmpty(shop.getAdmin().getUsername())
                ||StringUtils.isEmpty(shop.getAdmin().getPhone())
                ||StringUtils.isEmpty(shop.getAdmin().getEmail())
                ||StringUtils.isEmpty(shop.getAdmin().getPassword())
                ||StringUtils.isEmpty(shop.getAdmin().getConfirmPassword())


        ){
            //提示店铺商家，参数不能为空
            throw new BusinessException("参数不能为空");
        }

        Boolean aBoolean =BaiduAuditUtils.TextCensor(shop.getName());
        if(!aBoolean){
            throw new BusinessException("名称违规");
        }
        Employee admin = shop.getAdmin();
        //2.两次密码不一致校验
      if(!admin.getPassword().equals(admin.getConfirmPassword())){
            throw new BusinessException("两次密码输入不一致");
        }
        //3.店铺是否注册过校验
        Shop dbshop = shopMapper.getShopByMessage(shop);
        if(dbshop!=null){
            throw new BusinessException("该店铺已经被注册过");
        }
        //把shop 和 employee数据保存在表中
        //1.保存employee，获取自增的主键
        employeeMapper.save(admin);

        //2.把自增的主键设置到shop表中的admin_id中
        shop.setAdminId(admin.getId());
        //3.保存shop数据，返回自增的主键
        shopMapper.save(shop);
        //4.3 返回的自增主键设置给employee的shop_id字段
        admin.setShop_id(shop.getId());
        //5.更新employee数据
        employeeMapper.update(admin);

    }

    @Override
    public void pass(ShopLog log) throws MessagingException  { //log:shop_id, note
        //1.修改shop状态
        //通过shop_id查询shop数据，再修改状态
        Shop shop = shopMapper.loadById(log.getShop_id());

        //1.审核通过
        shop.setState(2);
        shopMapper.update(shop);

        //2.录入日志
        log.setState(2);
        log.setAudit_id(1L);
        shopLogMapper.save(log);


        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2856834773@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://dfs.java.itsource.cn/group1/M00/00/B9/rBE3kWD_fEuALou2AAB4rV4bI7c231.jpg' ><a href='http://localhost:8080/shop/active/"+log.getShop_id()+"'>点击该链接激活</a>",true);

        //收件人
        //获取收件人
        Employee employee = employeeMapper.loadById(shop.getAdminId());

        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);
    }



    @Override
    public void reject(ShopLog log) throws MessagingException  {
        Shop shop = shopMapper.loadById(log.getShop_id());

        //1.审核通过
        shop.setState(4);
        shopMapper.update(shop);

        //2.录入日志
        log.setState(4);
        log.setAudit_id(1L);
        shopLogMapper.save(log);


        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2856834773@qq.com");
        helper.setSubject("驳回邮件");
        helper.setText("<h1>你的店铺入驻失败!!!</h1><img src='http://dfs.java.itsource.cn/group1/M00/00/B9/rBE3kWD_fEuALou2AAB4rV4bI7c231.jpg' ><a href='http://localhost:8080/shop/active/"+log.getShop_id()+"'>点击该链接激活</a>",true);

        //收件人
        //获取收件人
        Employee employee = employeeMapper.loadById(shop.getAdminId());

        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);

        /**
         * 两种情况：
         *      1.修改状态为驳回 state=4;下次入驻的时候判断该店铺是否被驳回过，如果是就直接入驻
         *      2.直接删除shop employee
         */



    }

    @Override
    public List<ShopDto> getEcharts() {
        return shopMapper.getEcharts();
    }

    @Override
    public void patchInsert(List<Shop> list) {
        shopMapper.patchInsert(list);
    }
}
